﻿using IDAdmin.Common;
using IDAdmin.Lib.DataLayer;
using IDAdmin.Lib.UI;
using IDAdmin.Lib.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IDAdmin.Pages
{
    public partial class OnlinePeopleNumberManual : BasePage
    {
        private static List<Ccu> ccuData;
        protected override void Page_Load(object sender, EventArgs e)
        {

        }

        private Table
            getCcuRealTime()
        {
            //string _startDate = txtFromDate.Text;
            //string _endDate = txtToDate.Text;
            // IEnumerable<Ccu> ccuList=dt.AsEnumerable().ToList<Ccu>().;

            List<Ccu> dataList = new List<Ccu>(ccuData.Count);

            ccuData.ForEach((item) =>
            {
                dataList.Add(new Ccu(item));
            });

            //List<Ccu> dataList = new List<Ccu>(paramDt);//ConverterWorkData.ToList<Ccu>(paramDt).OrderBy(o=>o.Timestamp).ToList();
            foreach (Ccu ccuItem in dataList)
            {
                ccuItem.Timestamp = ConverterWorkData.truncateTime(ccuItem.Timestamp, "dd/MM/yyyy HH:mm");
            }
            //dataList = (from tb in dataList.AsParallel() where ConverterWorkData.ConvertFromUnixTimestamp(double.Parse(tb.Timestamp)).Minute % 5 == 0 select tb).ToList();
            dataList = (from tb in dataList.AsParallel() where ConverterWorkData.kindUnixTimeStampToKindDateTime(double.Parse(tb.Timestamp), DateTimeKind.Local, DateTimeKind.Local).Minute % 5 == 0 select tb).ToList();

            var gameZoneListTemp = dataList.AsParallel().Select(s => new { s.GameZone }).Distinct().OrderBy(o => o.GameZone).ToList();
            var gameZoneList = new[] { new { GameZone = "", ZoneName = "" } }.ToDictionary(d => d.GameZone, d => d.ZoneName);
            gameZoneList.Remove("");
            foreach (var zone in gameZoneListTemp)
            {
                if (gameZoneList.Keys.Contains(zone.GameZone) == false)
                {
                    gameZoneList.Add(zone.GameZone, dataList.AsParallel().Where(w => w.GameZone.Equals(zone.GameZone)).FirstOrDefault().ZoneName);
                }
            }
            Dictionary<int, List<Ccu>> gameZoneDataDic = new Dictionary<int, List<Ccu>>();
            for (int i = 0; i < gameZoneList.Count(); i++)
            {
                List<Ccu> gameZoneData = dataList.AsParallel().Where(w => w.GameZone.Equals(gameZoneList.ElementAt(i).Key)).OrderBy(o => o.Timestamp).ToList();
                gameZoneDataDic.Add(int.Parse(gameZoneList.ElementAt(i).Key), gameZoneData);
            }

            var timeList = dataList.AsParallel().OrderByDescending(o => o.Timestamp).Select(s => s.Timestamp).Distinct().ToList();
            //timeList = (from tb in timeList
            //where ConverterWorkData.ConvertFromUnixTimestamp(double.Parse(tb)).Minute % 5 == 0
            //select tb).ToList();
            var ccuPresentList = dataList.AsParallel().GroupBy(c => c.Timestamp).Select(g => new { Timestamp = g.Key, Sum = g.Sum(h => int.Parse(h.Num)) }).OrderBy(o => o.Timestamp).ToList();

            //var ccuDayBefore = dataList.Where(w => w.Timestamp.Equals(ConverterWorkData.ConvertToUnixTimestamp(DateTime.ParseExact(ConverterWorkData.ConvertFromUnixTimestamp(long.Parse(w.Timestamp)).AddDays(-1).ToString("dd/MM/yyyy hh:mm"),"dd/MM/yyyy hh:mm", CultureInfo.InvariantCulture)))).GroupBy(c => c.Timestamp).Select(g => new { Timestamp = (int.Parse(g.Key) + 86400).ToString(), Sum = g.Sum(h => int.Parse(h.Num)) }).OrderBy(o => o.Timestamp).ToList();
            //var CcuSevenDayBefore = dataList.Where(w => w.Timestamp.Equals((int.Parse(w.Timestamp) - 604800).ToString())).GroupBy(c => c.Timestamp).Select(g => new { Timestamp = (int.Parse(g.Key) + 604800).ToString(), Sum = g.Sum(h => int.Parse(h.Num)) }).OrderBy(o => o.Timestamp).ToList();
            //var CcuMonthBefore = dataList.Where(w => w.Timestamp.Equals((int.Parse(w.Timestamp) - 604800).ToString())).GroupBy(c => c.Timestamp).Select(g => new { Timestamp = (int.Parse(g.Key) + 604800).ToString(), Sum = g.Sum(h => int.Parse(h.Num)) }).OrderBy(o => o.Timestamp).ToList();

            List<CcuFiveMinuteOnce> CcuFiveMinuteOnceList = new List<CcuFiveMinuteOnce>();

            for (int i = 0; i < timeList.Count; i++)
            {
                //string strDt= ConverterWorkData.ConvertFromUnixTimestamp(long.Parse(timeList[i])).ToString("dd/MM/yyyy hh:mm");
                //DateTime dtTemp = DateTime.ParseExact(strDt, "dd/MM/yyyy hh:mm", CultureInfo.InvariantCulture);
                //string timeStampTemp = truncateSecond(timeList[i]);
                CcuFiveMinuteOnce cfmo = new CcuFiveMinuteOnce();
                foreach (KeyValuePair<int, List<Ccu>> entry in gameZoneDataDic)
                {
                    //var ccu = entry.Value.ToArray()[i];
                    Ccu ccu = null;
                    try
                    {
                        ccu = entry.Value.AsParallel().Where(w => w.Timestamp.Equals(timeList[i])).FirstOrDefault();
                        if (ccu == null)
                        {
                            ccu = new Ccu() { GameZone = entry.Key.ToString(), Timestamp = timeList[i], Num = "0" };
                        }
                    }
                    catch (Exception ex)
                    {

                        ccu = new Ccu() { GameZone = entry.Key.ToString(), Timestamp = timeList[i], Num = "0" };
                    }

                    cfmo.CcuServerList.Add(ccu);
                }
                try
                {
                    var ccuPresent = ccuPresentList.AsParallel().Where(w => w.Timestamp.Equals(timeList[i])).Single();
                    cfmo.SumCcuPresent = ccuPresent.Sum.ToString();
                }
                catch (Exception ex)
                {
                    cfmo.SumCcuPresent = "";
                }

                //if (ccuDayBefore.Count > 0)
                // {
                //cfmo.SumCcuDayBefore = ccuDayBefore.ToArray()[i].Sum.ToString();
                //cfmo.SumCcuDayBefore = ccuDayBefore.Where(w => w.Timestamp.Equals(timeList[i])).Select(s=>s.Sum).Single().ToString();

                //string strDateBeforeTs = truncateSecond(dayBeforeTs.ToString());
                try
                {
                    DateTime dayBeforedt = ConverterWorkData.kindUnixTimeStampToKindDateTime(double.Parse(timeList[i]), DateTimeKind.Local, DateTimeKind.Local).AddDays(-1);
                    long dayBeforeTs = long.Parse(ConverterWorkData.kindDateTimeToKindUnixTimestamp(dayBeforedt, DateTimeKind.Local).ToString());
                    var temp = dataList.AsParallel().Where(w => w.Timestamp.Equals(dayBeforeTs.ToString())).GroupBy(c => c.Timestamp).Select(g => new { Sum = g.Sum(h => int.Parse(h.Num)) }).Single();
                    cfmo.SumCcuDayBefore = temp.Sum.ToString();
                }
                catch (Exception ex)
                {
                    cfmo.SumCcuDayBefore = "";
                }

                try
                {
                    DateTime dayBeforedt = ConverterWorkData.kindUnixTimeStampToKindDateTime(double.Parse(timeList[i]), DateTimeKind.Local, DateTimeKind.Local).AddDays(-7);
                    long sevenDayBeforeTs = long.Parse(ConverterWorkData.kindDateTimeToKindUnixTimestamp(dayBeforedt, DateTimeKind.Local).ToString());
                    var temp = dataList.AsParallel().Where(w => w.Timestamp.Equals(sevenDayBeforeTs.ToString())).GroupBy(c => c.Timestamp).Select(g => new { Sum = g.Sum(h => int.Parse(h.Num)) }).Single();
                    cfmo.SumCcuSevenDayBefore = temp.Sum.ToString();
                }
                catch (Exception ex)
                {
                    cfmo.SumCcuSevenDayBefore = "";
                }

                try
                {
                    DateTime dayBeforedt = ConverterWorkData.kindUnixTimeStampToKindDateTime(double.Parse(timeList[i]), DateTimeKind.Local, DateTimeKind.Local).AddMonths(-1);
                    long thirtyDayBeforeTs = long.Parse(ConverterWorkData.kindDateTimeToKindUnixTimestamp(dayBeforedt, DateTimeKind.Local).ToString());
                    var temp = dataList.AsParallel().Where(w => w.Timestamp.Equals(thirtyDayBeforeTs.ToString())).GroupBy(c => c.Timestamp).Select(g => new { Sum = g.Sum(h => int.Parse(h.Num)) }).Single();
                    cfmo.SumCcuMonthBefore = temp.Sum.ToString();
                }
                catch (Exception ex)
                {
                    cfmo.SumCcuMonthBefore = "";
                }

                // }
                // if (CcuSevenDayBefore.Count > 0)
                // {
                // cfmo.SumCcuSevenDayBefore = CcuSevenDayBefore.ToArray()[i].Sum.ToString();
                // }
                // if (CcuMonthBefore.Count > 0)
                // {
                //cfmo.SumCcuMonthBefore = CcuMonthBefore.ToArray()[i].Sum.ToString();
                //}
                cfmo.DateTime = ConverterWorkData.kindUnixTimeStampToKindDateTime(long.Parse(timeList[i]), DateTimeKind.Local, DateTimeKind.Local);
                cfmo.Date = ConverterWorkData.kindUnixTimeStampToKindDateTime(long.Parse(timeList[i]), DateTimeKind.Local, DateTimeKind.Local).ToString("dd/MM/yyyy HH:mm");
                cfmo.TimeStamp = long.Parse(timeList[i]);
                CcuFiveMinuteOnceList.Add(cfmo);
            }
            //string startTime = timeList[0];

            //DataTable dt = ConverterWorkData.ToDataTable(CcuFiveMinuteOnceList, 0, gameZoneList.Count());

            Table table = new Table();
            table.CssClass = "table1";
            table.CellSpacing = 1;
            TableRow rowHeader = new TableRow();
            rowHeader.Cells.AddRange
            (
                new TableCell[]
                {     
                    UIHelpers.CreateTableCell("STT",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Thời gian",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Tổng cuu tháng trước",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Tổng cuu tuần trước",Unit.Percentage(10),HorizontalAlign.Center, "cellHeader"),  
                    UIHelpers.CreateTableCell("Tổng cuu ngày trước",Unit.Percentage(6),HorizontalAlign.Center, "cellHeader"),  
                    UIHelpers.CreateTableCell("Tổng cuu hiện tại",Unit.Percentage(6),HorizontalAlign.Center, "cellHeader")
                }
            );
            foreach (var zone in gameZoneList)
            {
                rowHeader.Cells.Add(UIHelpers.CreateTableCell(zone.Value, Unit.Percentage(6), HorizontalAlign.Center, "cellHeader"));
            }
            table.Rows.Add(rowHeader);
            try
            {
                if (CcuFiveMinuteOnceList == null || CcuFiveMinuteOnceList.Count == 0)
                {
                    TableRow rowEmpty = new TableRow();
                    rowEmpty.Cells.Add(UIHelpers.CreateTableCell("<p>Không có dữ liệu!</p>", HorizontalAlign.Center, "cell1", 4));
                    table.Rows.Add(rowEmpty);
                }
                else
                {
                    //labelCountOfUser.Text = string.Format("{0:N0}", dt.Rows.Count);
                    string css;
                    int stt = 0;
                    foreach (var dr in CcuFiveMinuteOnceList)
                    {
                        stt += 1;
                        css = stt % 2 == 0 ? "cell1" : "cell2";
                        TableRow row = new TableRow();
                        row.Cells.AddRange
                        (
                            new TableCell[]
                                {
                                    UIHelpers.CreateTableCell(stt.ToString(),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.Date),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.SumCcuMonthBefore),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.SumCcuSevenDayBefore), HorizontalAlign.Center, css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.SumCcuDayBefore),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.SumCcuPresent),HorizontalAlign.Center,css)
                                }
                        );
                        foreach (var zone in dr.CcuServerList.OrderBy(o => o.GameZone).ToList())
                        {
                            row.Cells.Add(UIHelpers.CreateTableCell(Converter.ToString(zone.Num), HorizontalAlign.Center, css));
                        }
                        table.Rows.Add(row);
                    }
                }
            }
            catch (Exception ex)
            {
            }

            return table;
        }
        private Table getCcuAverage()
        {
            List<Ccu> dataList = new List<Ccu>(ccuData.Count);

            ccuData.ForEach((item) =>
            {
                dataList.Add(new Ccu(item));
            });

            //List<Ccu> dataList = new List<Ccu>(paramDt);
            foreach (Ccu ccuItem in dataList)
            {
                ccuItem.Timestamp = ConverterWorkData.truncateTime(ccuItem.Timestamp, "dd/MM/yyyy");
            }
            var gameZoneListTemp = dataList.AsParallel().Select(s => new { s.GameZone }).Distinct().OrderBy(o => o.GameZone).ToList();
            var gameZoneList = new[] { new { GameZone = "", ZoneName = "" } }.ToDictionary(d => d.GameZone, d => d.ZoneName);
            gameZoneList.Remove("");
            foreach (var zone in gameZoneListTemp)
            {
                if (gameZoneList.Keys.Contains(zone.GameZone) == false)
                {
                    gameZoneList.Add(zone.GameZone, dataList.AsParallel().Where(w => w.GameZone.Equals(zone.GameZone)).FirstOrDefault().ZoneName);
                }
            }
            Dictionary<int, List<Acu>> gameZoneDataDic = new Dictionary<int, List<Acu>>();
            for (int i = 0; i < gameZoneList.Count(); i++)
            {
                List<Acu> gameZoneData = (from tb in dataList where tb.GameZone.Equals(gameZoneList.ElementAt(i).Key) group tb by new { tb.GameZone, tb.Timestamp } into grp select new Acu { GameZone = grp.Key.GameZone, Timestamp = grp.Key.Timestamp, Average = grp.Average(t => long.Parse(t.Num)).ToString("0") }).ToList(); //dataList.GroupBy(g => g.Timestamp, g => g.GameZone).Where(w => w.Key.Equals(gameZoneList.ElementAt(i))).OrderBy(o => o.Timestamp).ToList();
                gameZoneDataDic.Add(int.Parse(gameZoneList.ElementAt(i).Key), gameZoneData);
            }
            var timeList = dataList.OrderByDescending(o => o.Timestamp).Select(s => s.Timestamp).Distinct().ToList();
            //var acuSumList = dataList.GroupBy(c => c.Timestamp).Select(g => new { Timestamp = g.Key, Average = g.Average(h => int.Parse(h.Num)) }).OrderBy(o => o.Timestamp).ToList();

            List<CcuAverage> ccuAverageList = new List<CcuAverage>();

            for (int i = 0; i < timeList.Count; i++)
            {
                //string strDt= ConverterWorkData.ConvertFromUnixTimestamp(long.Parse(timeList[i])).ToString("dd/MM/yyyy hh:mm");
                //DateTime dtTemp = DateTime.ParseExact(strDt, "dd/MM/yyyy hh:mm", CultureInfo.InvariantCulture);
                //string timeStampTemp = truncateSecond(timeList[i]);
                CcuAverage ca = new CcuAverage();
                foreach (KeyValuePair<int, List<Acu>> entry in gameZoneDataDic)
                {
                    //var ccu = entry.Value.ToArray()[i];
                    Acu acu = null;
                    try
                    {
                        acu = entry.Value.Where(w => w.Timestamp.Equals(timeList[i])).FirstOrDefault();
                        if (acu == null)
                        {
                            acu = new Acu() { GameZone = entry.Key.ToString(), Timestamp = timeList[i], Average = "0" };
                        }
                    }
                    catch (Exception ex)
                    {

                        acu = new Acu() { GameZone = entry.Key.ToString(), Timestamp = timeList[i], Average = "0" };
                    }

                    ca.AverageCcuServerList.Add(acu);
                }
                try
                {
                    var acuSum = (from tb in ca.AverageCcuServerList group tb by new { tb.Timestamp } into grp select new { Average = grp.Sum(t => long.Parse(t.Average)) }).FirstOrDefault();
                    ca.AcuSum = acuSum.Average.ToString();
                }
                catch (Exception ex)
                {
                    ca.AcuSum = "";
                }
                ca.DateTime = ConverterWorkData.kindUnixTimeStampToKindDateTime(long.Parse(timeList[i]), DateTimeKind.Local, DateTimeKind.Local);
                ca.Date = ConverterWorkData.kindUnixTimeStampToKindDateTime(long.Parse(timeList[i]), DateTimeKind.Local, DateTimeKind.Local).ToString("dd/MM/yyyy");
                ca.TimeStamp = long.Parse(timeList[i]);
                ccuAverageList.Add(ca);
            }
            string startTime = timeList[0];

            //DataTable dt = ConverterWorkData.ToDataTable(ccuAverageList, 1, gameZoneList.Count());

            Table table = new Table();
            table.CssClass = "table1";
            table.CellSpacing = 1;
            TableRow rowHeader = new TableRow();
            rowHeader.Cells.AddRange
            (
                new TableCell[]
                {     
                    UIHelpers.CreateTableCell("STT",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Thời gian",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Tổng acu",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader")
                }
            );
            foreach (var zone in gameZoneList)
            {
                rowHeader.Cells.Add(UIHelpers.CreateTableCell(zone.Value, Unit.Percentage(6), HorizontalAlign.Center, "cellHeader"));
            }
            table.Rows.Add(rowHeader);
            try
            {
                if (ccuAverageList == null || ccuAverageList.Count == 0)
                {
                    TableRow rowEmpty = new TableRow();
                    rowEmpty.Cells.Add(UIHelpers.CreateTableCell("<p>Không có dữ liệu!</p>", HorizontalAlign.Center, "cell1", 4));
                    table.Rows.Add(rowEmpty);
                }
                else
                {
                    //labelCountOfUser.Text = string.Format("{0:N0}", dt.Rows.Count);
                    string css;
                    int stt = 0;
                    foreach (var dr in ccuAverageList)
                    {
                        stt += 1;
                        css = stt % 2 == 0 ? "cell1" : "cell2";
                        TableRow row = new TableRow();
                        row.Cells.AddRange
                        (
                            new TableCell[]
                                {
                                    UIHelpers.CreateTableCell(stt.ToString(),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.Date),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.AcuSum),HorizontalAlign.Center,css)
                                }
                        );
                        foreach (var zone in dr.AverageCcuServerList.OrderBy(o => o.GameZone).ToList())
                        {
                            row.Cells.Add(UIHelpers.CreateTableCell(Converter.ToString(zone.Average), HorizontalAlign.Center, css));
                        }
                        table.Rows.Add(row);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return table;
        }
        private Table getCcuMax()
        {
            List<Ccu> dataList = new List<Ccu>(ccuData.Count);

            ccuData.ForEach((item) =>
            {
                dataList.Add(new Ccu(item));
            });

            //List<Ccu> dataList = new List<Ccu>(paramDt);
            foreach (Ccu ccuItem in dataList)
            {
                ccuItem.Timestamp = ConverterWorkData.truncateTime(ccuItem.Timestamp, "dd/MM/yyyy");
            }
            var gameZoneListTemp = dataList.AsParallel().Select(s => new { s.GameZone }).Distinct().OrderBy(o => o.GameZone).ToList();
            var gameZoneList = new[] { new { GameZone = "", ZoneName = "" } }.ToDictionary(d => d.GameZone, d => d.ZoneName);
            gameZoneList.Remove("");
            foreach (var zone in gameZoneListTemp)
            {
                if (gameZoneList.Keys.Contains(zone.GameZone) == false)
                {
                    gameZoneList.Add(zone.GameZone, dataList.AsParallel().Where(w => w.GameZone.Equals(zone.GameZone)).FirstOrDefault().ZoneName);
                }
            }

            Dictionary<int, List<Pcu>> gameZoneDataDic = new Dictionary<int, List<Pcu>>();
            for (int i = 0; i < gameZoneList.Count(); i++)
            {
                List<Pcu> gameZoneData = (from tb in dataList where tb.GameZone.Equals(gameZoneList.ElementAt(i).Key) group tb by new { tb.GameZone, tb.Timestamp } into grp select new Pcu { GameZone = grp.Key.GameZone, Timestamp = grp.Key.Timestamp, Max = grp.Max(t => long.Parse(t.Num)).ToString() }).ToList(); //dataList.GroupBy(g => g.Timestamp, g => g.GameZone).Where(w => w.Key.Equals(gameZoneList.ElementAt(i))).OrderBy(o => o.Timestamp).ToList();
                gameZoneDataDic.Add(int.Parse(gameZoneList.ElementAt(i).Key), gameZoneData);
            }
            var timeList = dataList.OrderByDescending(o => o.Timestamp).Select(s => s.Timestamp).Distinct().ToList();
            //var acuSumList = dataList.GroupBy(c => c.Timestamp).Select(g => new { Timestamp = g.Key, Average = g.Average(h => int.Parse(h.Num)) }).OrderBy(o => o.Timestamp).ToList();

            List<CcuMax> pcuList = new List<CcuMax>();

            for (int i = 0; i < timeList.Count; i++)
            {
                //string strDt= ConverterWorkData.ConvertFromUnixTimestamp(long.Parse(timeList[i])).ToString("dd/MM/yyyy hh:mm");
                //DateTime dtTemp = DateTime.ParseExact(strDt, "dd/MM/yyyy hh:mm", CultureInfo.InvariantCulture);
                //string timeStampTemp = truncateSecond(timeList[i]);
                CcuMax cm = new CcuMax();
                foreach (KeyValuePair<int, List<Pcu>> entry in gameZoneDataDic)
                {
                    //var ccu = entry.Value.ToArray()[i];
                    Pcu pcu = null;
                    try
                    {
                        pcu = entry.Value.Where(w => w.Timestamp.Equals(timeList[i])).FirstOrDefault();
                        if (pcu == null)
                        {
                            pcu = new Pcu() { GameZone = entry.Key.ToString(), Timestamp = timeList[i], Max = "0" };
                        }
                    }
                    catch (Exception ex)
                    {

                        pcu = new Pcu() { GameZone = entry.Key.ToString(), Timestamp = timeList[i], Max = "0" };
                    }

                    cm.CcuMaxServerList.Add(pcu);
                }
                try
                {
                    var pcuSum = (from tb in cm.CcuMaxServerList group tb by new { tb.Timestamp } into grp select new { PcuSum = grp.Sum(t => long.Parse(t.Max)) }).FirstOrDefault();
                    cm.PcuSum = pcuSum.PcuSum.ToString();
                }
                catch (Exception ex)
                {
                    cm.PcuSum = "";
                }
                cm.DateTime = ConverterWorkData.kindUnixTimeStampToKindDateTime(long.Parse(timeList[i]), DateTimeKind.Local, DateTimeKind.Local);
                cm.Date = ConverterWorkData.kindUnixTimeStampToKindDateTime(long.Parse(timeList[i]), DateTimeKind.Local, DateTimeKind.Local).ToString("dd/MM/yyyy");
                cm.TimeStamp = long.Parse(timeList[i]);
                pcuList.Add(cm);
            }
            string startTime = timeList[0];

            //DataTable dt = ConverterWorkData.ToDataTable(pcuList, 2, gameZoneList.Count());

            Table table = new Table();
            table.CssClass = "table1";
            table.CellSpacing = 1;
            TableRow rowHeader = new TableRow();
            rowHeader.Cells.AddRange
            (
                new TableCell[]
                {     
                    UIHelpers.CreateTableCell("STT",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Thời gian",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    UIHelpers.CreateTableCell("Tổng pcu",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                    //UIHelpers.CreateTableCell("CcuServerList",Unit.Percentage(6),HorizontalAlign.Center, "cellHeader"),  
                    //UIHelpers.CreateTableCell(dt.Columns[5].ColumnName,Unit.Percentage(6),HorizontalAlign.Center, "cellHeader"),  
                    //UIHelpers.CreateTableCell(dt.Columns[6].ColumnName,Unit.Percentage(6),HorizontalAlign.Center, "cellHeader")
                }
            );
            foreach (var zone in gameZoneList)
            {
                rowHeader.Cells.Add(UIHelpers.CreateTableCell(zone.Value, Unit.Percentage(6), HorizontalAlign.Center, "cellHeader"));
            }
            table.Rows.Add(rowHeader);
            try
            {
                if (pcuList == null || pcuList.Count == 0)
                {
                    TableRow rowEmpty = new TableRow();
                    rowEmpty.Cells.Add(UIHelpers.CreateTableCell("<p>Không có dữ liệu!</p>", HorizontalAlign.Center, "cell1", 4));
                    table.Rows.Add(rowEmpty);
                }
                else
                {
                    //labelCountOfUser.Text = string.Format("{0:N0}", dt.Rows.Count);
                    string css;
                    int stt = 0;
                    foreach (var dr in pcuList)
                    {
                        stt += 1;
                        css = stt % 2 == 0 ? "cell1" : "cell2";
                        TableRow row = new TableRow();
                        row.Cells.AddRange
                        (
                            new TableCell[]
                           {
                                    UIHelpers.CreateTableCell(stt.ToString(),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.Date),HorizontalAlign.Left,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.PcuSum),HorizontalAlign.Left,css)
                           }
                        );
                        foreach (var zone in dr.CcuMaxServerList.OrderBy(o => o.GameZone).ToList())
                        {
                            row.Cells.Add(UIHelpers.CreateTableCell(Converter.ToString(zone.Max), HorizontalAlign.Left, css));
                        }
                        table.Rows.Add(row);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return table;
        }
        protected void countButton_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            watch.Start();

            if (countTypeDropDownList.SelectedValue.Equals("0"))
            {
                dataPanel.Controls.Clear();
                dataPanel.Controls.Add(getCcuRealTime());
            }
            else
                if (countTypeDropDownList.SelectedValue.Equals("1"))
                {
                    dataPanel.Controls.Clear();
                    dataPanel.Controls.Add(getCcuAverage());
                }
                else
                    if (countTypeDropDownList.SelectedValue.Equals("2"))
                    {
                        dataPanel.Controls.Clear();
                        dataPanel.Controls.Add(getCcuMax());
                    }

            watch.Stop();
            double elapsedMS = watch.ElapsedMilliseconds;
            measureLB.Text = elapsedMS.ToString() + "miliseconds";

            WebDB.WriteLog(_User.UserName, Request.UserHostAddress, "Count " + countTypeDropDownList.SelectedItem.Text + " từ ngày:" + countToolBar.beginDateTextBox.Text + " đến ngày:" + countToolBar.toDateTextBox.Text);
        }

        protected void getCcuButton_Click(object sender, EventArgs e)
        {

            DateTime dtFormDate = IDAdmin.Common.ConverterWorkData.convertStringToDateTime(countToolBar.beginDateTextBox.Text);
            DateTime dtToDate = IDAdmin.Common.ConverterWorkData.convertStringToDateTime(countToolBar.toDateTextBox.Text);
            List<DateTime> dateTimeList = IDAdmin.Common.ConverterWorkData.getDateListBetween(dtFormDate, dtToDate);
            ccuData = WebDB.getCcu(dateTimeList);
        }
    }
}